<?php
namespace Home\Controller;
use Think\Controller;
class AdminController extends BaseController {
    public function admin_list(){
        $data = D("Admin")->search();
        $count = M("Admin")->count();
//        print_r($data['data']);
        $this->assign(array(
            'data' => $data['data'],
            'page' => $data['page'],
            'count'=> $count,
        ));
        $this->display('index');
    }
    public function admin_add(){
        if(IS_AJAX){
            $data['password'] = getMd5Password(I("post.password"));
            $data['username'] = trim(I('post.username'));
            $data['realname'] = trim(I('post.realname'));
            $data['gender'] = trim(I('post.gender'));
            $data['phonenumber'] = trim(I('post.phonenumber'));
            $data['email'] = trim(I('post.email'));
            $data['is_use'] = trim(I('post.is_use'));
            $data['lastlogintime'] = time();
            if(D('Admin')->add($data)){
                show(1,'数据添加成功');
            }else{
                show(0,'添加失败');
            }
        }else{
            //取出所有的角色
            $roleModel = M('Role');
            $roleData = $roleModel->select();
            $this->assign('roleData',$roleData);
            $this->display('add');
        }
    }
    public function edit(){
        $id = I('get.id');//要编辑的管理员
        $arr = session('adminUser');
        $loginAdminId = $arr['id'];
        if($loginAdminId>1 && $loginAdminId!=$id && $loginAdminId != I('post.id')){
            exit("<h1 style='color:red'>你无权限操作,你只能修改自己的信息</h1>");
        }
        if(IS_AJAX){
            $data['password'] = getMd5Password(trim(I('post.password')));
            if(empty($data['password'])){
                unset($data['password']);
            }
            $data['id'] = trim(I('post.id'));
            $data['username'] = trim(I('post.username'));
            $data['realname'] = trim(I('post.realname'));
            $data['gender'] = trim(I('post.gender'));
            $data['phonenumber'] = trim(I('post.phonenumber'));
            $data['email'] = trim(I('post.email'));
            $data['is_use'] = trim(I('post.is_use'));
            if(D("Admin")->save($data) !== false){  //如果说什么都没有改,这张表就会返回0(这里的0指的是受影响的行数)
                show(1,'修改成功');
            }else{
                show(0,'修改失败');
            }
        }else{
            //编辑的这个管理员的基本信息
            $AdminData = D('Admin')->find($id);
            $this->assign('AdminData',$AdminData);
    //      print_r($AdminData);

            //取出所有的角色
            $roleModel = M('Role');
            $roleData = $roleModel->select();
            $this->assign('roleData',$roleData);

            //取出当前管理员所在的角色id
            $arModel = M('AdminRole');
            $rid = $arModel->field('GROUP_CONCAT(role_id) role_id')->where(array('admin_id'=>array('eq',$id)))->find();
            $rid = explode(',',$rid['role_id']);
            $this->assign('rid',$rid);
            $this->display();
        }
    }
    //管理员停用
    public function updateis_use(){
        $data = $_POST;
        if(M("Admin")->save($data)){
            show(1,'','');
        }else{
            show(0,'','');
        }
    }
    public function del(){
        $id = I('post.id',0);  //如果没有参数就是默认值0
        $ids = D("Admin")->delete($id);  //删除之前有钩子函数
        if($ids!=0){//删除成功
            return show(1,'删除成功!');
        }else{
            return show(0,'删除失败!');
        }
    }
}